﻿using System;
using System.Collections.Generic;
using System.Text;
using FluentData;
using System.Dynamic;
using System.Dynamic;

namespace Eleanor.DAL
{
    public class TagDAL : DALBase
    {
        public List<dynamic> SelectAll()
        {
            var command = DefaultContext
                        .Sql("TagSelectALL")
                        .CommandType(DbCommandTypes.StoredProcedure);
            return command.QueryMany<dynamic>();
        }

        public dynamic Select(string tagName)
        {
            const string sql = @"select * from tag where value=@0";
            return CustomMapperUsingDynamic(DefaultContext.Sql(sql).Parameters(tagName).QuerySingle<dynamic>());
        }

        public List<dynamic> SelectByPaintingId(int paintingId)
        {
            return DefaultContext.Sql(@"select t.tag_id as TagId, t.value as Value from 
                painting_tag pt inner join tag t on pt.painting_id=@0 and t.tag_id=pt.tag_id")
                               .Parameters(paintingId)
                               .QueryMany<dynamic>();
        }

        public dynamic CustomMapperUsingDynamic(dynamic row)
        {
            if (row == null)
            {
                return null;
            }
            dynamic tag = new ExpandoObject();
            tag.TagId = row.tag_id;
            tag.Value = row.value;
            tag.CountOfPaint = row.count_of_paint;
            tag.CreateDate = row.create_date;
            tag.UpdateDate = row.update_date;
            return tag;
        }   
    }
}
